const app = getApp();

import {
    getIndexData,
    getCoupons
} from '../../api/api.js';
import {
    getSeckillIndexTime,
    getSeckillList
} from '../../api/activity.js';
import Util from '../../utils/util.js';
import wxh from '../../utils/wxh.js';
Page({
    /**
     * 页面的初始数据
     */
    data: {
        imgUrls: [],
        itemNew: [],
        activityList: [],
        menus: [],
        bastBanner: [],
        bastInfo: '',
        bastList: [],
        fastInfo: '',
        fastList: [],
        firstInfo: '',
        firstList: [],
        salesInfo: '',
        likeInfo: [],
        lovelyBanner: {},
        benefit: [],
        indicatorDots: true,
        circular: true,
        autoplay: true,
        interval: 3000,
        duration: 500,
        parameter: {
            'navbar': '0',
            'return': '0'
        },
        window: false,
        topImage: '',
        seckillList: [],
        timeList: [],
        active: 5,
        scrollLeft: 0,
        interval: 0,
        countDownHour: "00",
        countDownMinute: "00",
        countDownSecond: "00",
        page: 1,
        limit: 20,
        loading: false,
        loadend: false,
        pageloading: false,
        iShidden: true,
        isAuto: false,
        where: {
            longitude: '',
            latitude: '',
            district: ''
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        if (options.spid) app.globalData.spid = options.spid;
        if (options.scene) app.globalData.code = decodeURIComponent(options.scene);
        this.getSeckillConfig();
    },
    allowFun: function() {
        Util.getlocation(this)
        this.getSeckillConfig();
        this.getIndexConfig();
    },
    purchase: function(e) {
        this.setData({
            isAuto: true,
            iShidden: false
        });

    },
    go_top: function() {
        if (wx.pageScrollTo) {
            wx.pageScrollTo({
                scrollTop: 0
            })
        } else {
            wx.showModal({
                title: '提示',
                content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
            })
        }

    },
    goodscate: function(e) {
        var url = e.currentTarget.dataset.url;
        if (url.indexOf("goods_cate") != -1) {
            var cate_id = url.substring(37);
            url = url.substring(0, 28);
            app.globalData.cate_id = cate_id;
        }
        wx.switchTab({
            url: url
        });
    },
    catchTouchMove: function(res) {
        return false
    },
    onColse: function() {
        this.setData({
            window: false
        });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        Util.getlocation(this)
        this.getIndexConfig();
        if (app.globalData.isLog && app.globalData.token) this.get_issue_coupon_list();
    },
    get_issue_coupon_list: function() {
        var that = this;
        getCoupons({
            page: 1,
            limit: 3
        }).then(res => {
            that.setData({
                couponList: res.data
            });
            if (!res.data.length) that.setData({
                window: false
            });
        });
    },
    getIndexConfig: function() {
        var that = this;
        getIndexData(that.data.where).then(res => {
            var item_width
            if (res.data.guide == '5') {
                item_width = 'width20'
            } else {
                item_width = 'width25'
            }
            that.setData({
                imgUrls: res.data.banner,
                menus: res.data.menus,
                item_width: item_width,
                itemNew: res.data.roll,
                activityList: res.data.activity,
                bastBanner: res.data.info.bastBanner,
                bastInfo: res.data.info.bastInfo,
                bastList: res.data.info.bastList,
                fastInfo: res.data.info.fastInfo,
                fastList: res.data.info.fastList,
                firstInfo: res.data.info.firstInfo,
                firstList: res.data.info.firstList,
                salesInfo: res.data.info.salesInfo,
                likeInfo: res.data.likeInfo,
                lovelyBanner: res.data.lovely.length ? res.data.lovely[0] : {},
                benefit: res.data.benefit,
                logoUrl: res.data.logoUrl,
                couponList: res.data.couponList
            });

            app.globalData.cart_count = res.data.cart_count.toString();
            if (app.globalData.cart_count > 0) {
                wx.setTabBarBadge({
                    index: 3,
                    text: app.globalData.cart_count
                })
            }

            wx.getSetting({
                success(res) {
                    if (!res.authSetting['scope.userInfo']) {
                        that.setData({
                            window: that.data.couponList.length ? true : false
                        });
                    } else {
                        that.setData({
                            window: false
                        });
                    }
                }
            });
        })
    },
    goDetails: function(e) {
        if (e.currentTarget.dataset.status == '1'){
            wx.navigateTo({
                url: '/pages/activity/goods_seckill_details/index?id=' + e.currentTarget.dataset.id + '&time=' + this.data.timeList[this.data.active].stop + '&status=' + e.currentTarget.dataset.status,
            })
        }else{
            wx.navigateTo({
                url: '/pages/goods_details/index?id=' + e.currentTarget.dataset.product_id,
            })
        }
        
    },
    settimeList: function(e) {
        var that = this;
        that.setData({
            active: e.currentTarget.dataset.index
        });
        if (that.data.interval) {
            clearInterval(that.data.interval);
            that.setData({
                interval: null
            });
        }
        that.setData({
            interval: 0,
            countDownHour: "00",
            countDownMinute: "00",
            countDownSecond: "00",
            status: that.data.timeList[that.data.active].status,
            loadend: false,
            page: 1,
            seckillList: [],
        });
        wxh.time(e.currentTarget.dataset.stop, that);
        that.getSeckillList();
    },
    getSeckillConfig: function() {
        let that = this;
        getSeckillIndexTime().then(res => {
            that.setData({
                topImage: res.data.lovely,
                timeList: res.data.seckillTime,
                active: res.data.seckillTimeIndex
            });
            if (that.data.timeList.length) {
                wxh.time(that.data.timeList[that.data.active].stop, that);
                that.setData({
                    scrollLeft: (that.data.active - 1.37) * 100
                });
                setTimeout(function() {
                    that.setData({
                        loading: true
                    })
                }, 2000);
                that.setData({
                    seckillList: [],
                    offset: 0
                });
                that.setData({
                    status: that.data.timeList[that.data.active].status
                });
                that.getSeckillList();
            }
        });
    },
    getSeckillList: function() {
        var that = this;
        var data = {
            page: 1,
            limit: 2,
            latitude: that.data.where.latitude,
            longitude: that.data.where.longitude,
            district: that.data.where.district
        };
        if (that.data.pageloading) return;
        that.setData({
            pageloading: true
        });
        getSeckillList(that.data.timeList[that.data.active].id, data).then(res => {
            var seckillList = that.data.seckillList;
            that.setData({
                seckillList: seckillList.concat(res.data),
                pageloading: false
            });
        }).catch(err => {
            that.setData({
                pageloading: false
            });
        });
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {
        this.setData({
            window: false
        });
        if (this.data.interval) {
            clearInterval(this.data.interval);
            this.setData({
                interval: null
            });
        }
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        this.getIndexConfig();
        if (app.globalData.isLog && app.globalData.token) this.get_issue_coupon_list();
        wx.stopPullDownRefresh();
    },


    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})